/**
 * 
 */
package eu.reply.android.flashm.common;

import java.io.UnsupportedEncodingException;

import android.util.Log;

/**
 * An instance of this class is created as the result of a {@link HttpConnection}
 * or {@link HttpAsyncConnection}. It is not intended to be instantiated manually
 * 
 * @author Cuauhtemoc Delgado
 *
 */
public class HttpResponse {

	private static final String TAG="HttpResponse";
	
	int statusCode;
	String contentType;
	String contentEncoding;
	byte[] content;
	String location;
	
	HttpError error;
	/**
	 * Constructor to be used within a {@link HttpConnection} or {@link HttpAsyncConnection}
	 * to represent the result of the connection. It is not indented to be used outside the
	 * instances of HTTP connections.
	 * 
	 * @param statusCode The HTTP status code received
	 * @param content The content of the HTTP message
	 * @param contentType The MIME type associated to {@code content}
	 * @param contentEncoding The encoding associated to {@code content}
	 */
	public HttpResponse(int statusCode,byte[] content,String contentType,String contentEncoding){
		this.statusCode=statusCode;
		this.content=content;
	}

	/**
	 * Get the MIME type associated to the content in this response
	 * @return The MIME type of the content or null if there is no content-type associated
	 * @see #getContent()
	 */
	public String getContentType() {
		return contentType;
	}

	/**
	 * Returns the content encoding type associated to the response 
	 * or null if there was no encoding specified.
	 * 
	 * @return the encoding of the content (i.e. UTF-8)
	 */
	public String getContentEncoding() {
		return contentEncoding;
	}

	/**
	 * Return the content received as result of the Http Connection.
	 * The content can be converted to a string like this:
	 * 
	 * <i>String JSONString = new String(httpResp.getContent(), httpResp.getContentEncoding())</i>
	 * 		
	 * @return The content of http response
	 */
	public byte[] getContent() {
		return content;
	}

	/**
	 * Get the status of the HTTP response (e.g 200)
	 * @return the status code or -1 if no valid response was received
	 */
	public int getStatusCode() {
		return statusCode;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	/**
	 * If the statusCode is an error value then this error specifies
	 * what the error problem was
	 * @return
	 */
	public HttpError getError() {
		return error;
	}

	protected void setError(HttpError error) {
		this.error = error;
	}
	
	public String toString(){
		String resp="Did not read the HTTP resposne as string";
		//HTTP success
		byte []httpPayload = this.getContent();
		String encoding=this.getContentEncoding()==null?"UTF-8":this.getContentEncoding();
		try {
			resp = new String(httpPayload, encoding);
		} catch (UnsupportedEncodingException e) {
			// TODO unsupported encoding specified by the server
			Log.e(TAG,"Unsuported encoding: "+encoding);
			resp=encoding;
		} 
		return resp;
	}
	
	
}
